package com.smooth.mall.controller;


import com.smooth.mall.common.ApiRestResponse;
import com.smooth.mall.exception.SmoothMallException;
import com.smooth.mall.exception.SmoothMallExceptionEnum;
import com.smooth.mall.service.UserService;
import com.smooth.mall.model.pojo.User;
import com.smooth.mall.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/test")
    @ResponseBody
    public User personalPage(Integer id){
        return userService.getUser(id);
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("username") String username,
                                    @RequestParam("password") String password) throws SmoothMallException {
        //username与password的空值校验
        if(StringUtils.isEmpty(username)){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(password)){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度校验。密码长度不能少于8位
        if(password.length()<8){
            return ApiRestResponse.error(SmoothMallExceptionEnum.PASSWORD_TOO_SHORT);
        }

        userService.register(username,password);
        return ApiRestResponse.success();
    }

    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("username") String username,
                                 @RequestParam("password") String password, HttpSession session) throws SmoothMallException {
        //username与password的空值校验
        if(StringUtils.isEmpty(username)){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(password)){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(username, password);
        //保存用户信息时，不保存密码
        user.setPassword(null);
        session.setAttribute(Constant.SMOOTH_MALL_USER,user);
        return ApiRestResponse.success(user);
    }

    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam String signature) throws
            SmoothMallException {
        User currentUser = (User)session.getAttribute(Constant.SMOOTH_MALL_USER);
        if(currentUser==null){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInformation(user);
        return ApiRestResponse.success();
    }

    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session){
        session.removeAttribute(Constant.SMOOTH_MALL_USER);
        return ApiRestResponse.success();
    }
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName,
                                      @RequestParam("password") String password, HttpSession session)
            throws SmoothMallException {
        if (StringUtils.isEmpty(userName)) {
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(userName, password);
        //校验是否是管理员
        if (userService.checkAdminRole(user)) {
        //是管理员，执行操作
        //保存用户信息时，不保存密码
            user.setPassword(null);
            session.setAttribute(Constant.SMOOTH_MALL_USER, user);
//            System.out.println(session.getAttribute(Constant.SMOOTH_MALL_USER));
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_ADMIN);
        }
    }

}
